﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceWarrantyRecordAccessoriesService
    {
        public IQueryable<InsuranceWarrantyRecordAccessories> LoadByInsuranceWarrantyRecordId(long insuranceWarrantyRecordId)
        {
            return LoadEntities(o => o.InsuranceWarrantyRecordID == insuranceWarrantyRecordId);
        }

        public InsuranceWarrantyRecordAccessories Load(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public bool Save(List<InsuranceWarrantyRecordAccessories> insuranceWarrantyRecordAccessoriesList, string operateUser)
        {
            var result = true;
            if (insuranceWarrantyRecordAccessoriesList.Any())
            {
                for (int i = 0; i < insuranceWarrantyRecordAccessoriesList.Count; i++)
                {
                    if (!result)
                        return false;
                    var insuranceWarrantyRecordAccessories = insuranceWarrantyRecordAccessoriesList[i];
                    if (insuranceWarrantyRecordAccessories.ID > 0 && insuranceWarrantyRecordAccessories.AccessoriesTypeID > 0 && insuranceWarrantyRecordAccessories.AccessoriesLevelID > 0)
                    {
                        var entity =
                            LoadByInsuranceWarrantyRecordId(
                                insuranceWarrantyRecordAccessories.InsuranceWarrantyRecordID).FirstOrDefault();//Load(insuranceWarrantyRecordAccessories.ID);
                        if (entity != null)
                        {
                            entity.AccessoriesLevelID = insuranceWarrantyRecordAccessories.AccessoriesLevelID;
                            entity.AccessoriesTypeID = insuranceWarrantyRecordAccessories.AccessoriesTypeID;
                            entity.AccessoriesPrice = insuranceWarrantyRecordAccessories.AccessoriesPrice;
                            result = UpdateEntities(entity);
                        }
                        else
                        {
                            insuranceWarrantyRecordAccessories.CreatedBy = operateUser;
                            insuranceWarrantyRecordAccessories.CreatedTime = DateTime.Now;
                            result = AddEntities(insuranceWarrantyRecordAccessories).ID > 0;
                        }
                    }
                }
            }
            return result;
        }
    }
}
